{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# LU Factorization"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import numpy.linalg as la"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Part 1: One Column of LU"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[-2.,  2., -1.],\n",
              "       [-3.,  1., -9.],\n",
              "       [-5., -5., -2.]])"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n = 3\n",
        "\n",
        "np.random.seed(15)\n",
        "A = np.round(5*np.random.randn(n, n))\n",
        "\n",
        "A"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize `L` and `U` with zeros:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "L = np.zeros((n,n))\n",
        "U = np.zeros((n,n))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Set `U` to be the first row of `A`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[-2.,  2., -1.],\n",
              "       [ 0.,  0.,  0.],\n",
              "       [ 0.,  0.,  0.]])"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "U[0,:] = A[0,:]\n",
        "U"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compute the first column of `L`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1. ,  0. ,  0. ],\n",
              "       [ 1.5,  0. ,  0. ],\n",
              "       [ 2.5,  0. ,  0. ]])"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "L[:,0] = A[:,0]/U[0,0]\n",
        "L"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compare what we have to `A`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 59,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[-2.  2. -1.]\n",
            " [-3.  1. -9.]\n",
            " [-5. -5. -2.]]\n",
            "[[-2.   2.  -1. ]\n",
            " [-3.   3.  -1.5]\n",
            " [-5.   5.  -2.5]]\n"
          ]
        }
      ],
      "source": [
        "print(A)\n",
        "print(L@U)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Perform the Schur complement update and store the result in `A1`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 61,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[  0. ,   0. ,   0. ],\n",
              "       [  0. ,  -2. ,  -7.5],\n",
              "       [  0. , -10. ,   0.5]])"
            ]
          },
          "execution_count": 61,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "A1 = A - L @ U\n",
        "A1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Take the second row of `U` to be the second row of `A1`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 62,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[-2. ,  2. , -1. ],\n",
              "       [ 0. , -2. , -7.5],\n",
              "       [ 0. ,  0. ,  0. ]])"
            ]
          },
          "execution_count": 62,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "U[1,1:] = A1[1,1:]\n",
        "U"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We can now compute the next column of `L`:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 64,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([[ 1. ,  0. ,  0. ],\n",
              "       [ 1.5,  1. ,  0. ],\n",
              "       [ 2.5,  5. ,  0. ]])"
            ]
          },
          "execution_count": 64,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "L[1:,1] = A1[1:,1]/U[1,1]\n",
        "L"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "And finally, compute the bottom right elements of `L` and `U`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 65,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "U[2,2] = A1[2,2] - L[2,1]*U[1,2]\n",
        "L[2,2] = 1.0"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 66,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[ 1.   0.   0. ]\n",
            " [ 1.5  1.   0. ]\n",
            " [ 2.5  5.   1. ]]\n",
            "[[ -2.    2.   -1. ]\n",
            " [  0.   -2.   -7.5]\n",
            " [  0.    0.   38. ]]\n"
          ]
        }
      ],
      "source": [
        "print(L)\n",
        "print(U)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 67,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[-2.  2. -1.]\n",
            " [-3.  1. -9.]\n",
            " [-5. -5. -2.]]\n",
            "[[-2.  2. -1.]\n",
            " [-3.  1. -9.]\n",
            " [-5. -5. -2.]]\n"
          ]
        }
      ],
      "source": [
        "print(A)\n",
        "print(L@U)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Part 2: The Full Algorithm\n",
        "\n",
        "Implement the general LU factorization algorithm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "collapsed": true
      },
      "outputs": [],
      "source": [
        "n = 4\n",
        "A = np.random.random((n,n)) \n",
        "L = np.zeros((n,n)) \n",
        "U = np.zeros((n,n)) \n",
        "M = A.copy()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 108,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "for i in range(n):\n",
        "    U[i,i:] = M[i,i:]\n",
        "    L[i:,i] = M[i:,i]/U[i,i]\n",
        "    M[i+1:,i+1:] -= np.outer(L[i+1:,i:i+1],U[i:i+1,i+1:])   "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 110,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[ 1.          0.          0.          0.        ]\n",
            " [ 0.48283957  1.          0.          0.        ]\n",
            " [ 1.0179142   0.08898352  1.          0.        ]\n",
            " [ 0.08538441  0.70860694 -0.76449667  1.        ]]\n",
            "[[ 0.81336415  0.0571982   0.97858297  0.90071912]\n",
            " [ 0.          0.86381599  0.00137876  0.32479681]\n",
            " [ 0.          0.         -0.66417473 -0.90299951]\n",
            " [ 0.          0.          0.         -0.03547261]]\n",
            "[[  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]\n",
            " [  0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]\n",
            " [ -1.11022302e-16   0.00000000e+00   0.00000000e+00   0.00000000e+00]\n",
            " [  0.00000000e+00   0.00000000e+00   0.00000000e+00   1.11022302e-16]]\n"
          ]
        }
      ],
      "source": [
        "print(L)\n",
        "print(U)\n",
        "print(A-L@U)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.5.3"
    },
    "widgets": {
      "state": {},
      "version": "1.1.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}